package com.wendyxie.sproutup.view.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.wendyxie.sproutup.databinding.ItemScoreAddBinding
import com.wendyxie.sproutup.db.SQLInitHelper
import com.wendyxie.sproutup.db.SQLUserDomainHelper
import com.wendyxie.sproutup.db.SQLUserScoreRecordHelper
import com.wendyxie.sproutup.db.SQLUserScoreTmplHelper
import com.wendyxie.sproutup.entity.UserScoreRecordEntity
import com.wendyxie.sproutup.entity.UserScoreTmplEntity
import com.wendyxie.sproutup.utils.SPUtil

class ScoreTmplAdapter : RecyclerView.Adapter<ScoreTmplAdapter.ScoreViewHolder>() {

    private var scores = mutableListOf<UserScoreTmplEntity>()
    private lateinit var binding: ItemScoreAddBinding

    class ScoreViewHolder(private val binding: ItemScoreAddBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(score: UserScoreTmplEntity) {
            binding.tvScore.text = score.score.toString() + "g"
            binding.tvScoreTitle.text = score.scoreTitle
            if (score.isAuto != 0) {
                binding.btnAdd.visibility = View.GONE
                binding.tvAuto.visibility = View.VISIBLE
            } else {
                binding.btnAdd.visibility = View.VISIBLE
                binding.tvAuto.visibility = View.GONE
            }
            // 添加按钮
            binding.btnAdd.setOnClickListener {
                clickAddBtn(score)
            }
        }

        private fun clickAddBtn(score: UserScoreTmplEntity) {
            // 入库
            try {
                SQLUserDomainHelper.addUserScoreRecord(
                    UserScoreRecordEntity(SPUtil.getUserId(binding.root.context), SPUtil.getBabyId(binding.root.context),score.score, score.scoreTitle, score.id),
                    SPUtil.getBabyId(binding.root.context),
                    SQLInitHelper(binding.root.context).openDatabase()
                )
                Toast.makeText(binding.root.context, "添加成功", Toast.LENGTH_SHORT).show()
            } catch (e: Exception) {
                e.printStackTrace()
                Log.d("AddScoreTmplActivity", "clickAddBtn: ${e.message}")
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ScoreViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        binding = ItemScoreAddBinding.inflate(layoutInflater, parent, false)
        binding.btnAdd.setOnClickListener {
            Toast.makeText(parent.context, "Add", Toast.LENGTH_SHORT).show()
        }

        return ScoreViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ScoreViewHolder, position: Int) {
        holder.bind(scores[position])
    }

    override fun getItemCount(): Int = scores.size

    fun setItems(items: List<UserScoreTmplEntity>) {
        scores.clear()
        scores.addAll(items)
        notifyDataSetChanged()
    }

    // 删除某列
    fun removeItem(position: Int) {
        // 删除数据库内容
        SQLUserScoreTmplHelper.deleteUserScoreTmpl(scores[position].id!!, SQLInitHelper(binding.root.context).openDatabase())
        scores.removeAt(position)
        notifyItemRemoved(position)
    }
}
